﻿"use strict";

function Tileset() {
}
Tileset.prototype.filename = null;
Tileset.prototype.map = null;
Tileset.prototype.image = null;
Tileset.prototype.tilesize = 32;
Tileset.prototype.Add = function (filename, xys, ox, oy) {
    this.image = new Image();
    this.filename = filename;
    this.image.src = filename;
    var tiles = [];
    for (var i = 0; i < xys.length; i++) {
        if (xys[i].length < 4)
            tiles.push([xys[i][0], xys[i][1], this.tilesize, this.tilesize, ox, oy]);
        else if (xys[i].length < 6)
            tiles.push([xys[i][0], xys[i][1], xys[i][2], xys[i][3], ox, oy]);
        else
            tiles.push([xys[i][0], xys[i][1], xys[i][2], xys[i][3], xys[i][4], xys[i][5]]);
    }
    this.map = tiles;
}





function Library()
{
    var bextiles = new Tileset();
    bextiles.Add('data/Graphics/Sarah/bexhill overhead full sheet vers 3.png', [
        [0, 0], [32, 0], [64, 0],
        [0, 32], [32, 32], [64, 32],
        [0, 64], [32, 64], [64, 64],
        [0, 96], [32, 96], [64, 96],
    ], 0, 0);

    var hoot = new Tileset();
    hoot.Add('data/Graphics/Sarah/mc overhead full sheet vers 1.png', [
        [0, 0], [32, 0], [64, 0],
        [0, 32], [32, 32], [64, 32],
        [0, 64], [32, 64], [64, 64],
        [0, 96], [32, 96], [64, 96],
    ], 0, 0);

    var lady = new Tileset();
    lady.Add('data/Graphics/Characters/wiselady.png', [
        [0, 0], [32, 0], [64, 0],
        [0, 32], [32, 32], [64, 32],
        [0, 64], [32, 64], [64, 64],
        [0, 96], [32, 96], [64, 96],
    ], 0, 0);

    this.tiles = {
        bextiles: bextiles,
        lady: lady,
        hoot: hoot
    };
};
Library.prototype.graphics = {};
Library.prototype.pending = {};
Library.prototype.nexttime = 0;
Library.prototype.LoadGraphic = function (filename) {
    if (filename in this.graphics)
        return this.graphics[filename];
    if (filename in this.pending)
        return null;
    log(new Date().getTime().toString() + 'Library: Loading ' + filename);
    var img = new Image();
    this.pending[filename] = img;
    img.src = filename;
    return null;
}
Library.prototype.Tick = function (now) {
    for (var filename in this.pending) {
        var img = this.pending[filename];
        if (img.complete && img.width > 0 && img.height > 0) {
            img.w = img.width;
            img.h = img.height;
            log('Library: Done loading ' + filename);
            this.graphics[filename] = img;
            delete this.pending[filename];
        }
    }
    if (now > this.nexttime) {
        this.nexttime = now + 30000; // Every 30 seconds,
        this.Purge(now, 600000);     // purge images older than 10 minutes.
    }
}
Library.prototype.Purge = function (now, numseconds) {
    var asof = now - numseconds;
    for (var key in this.graphics) {
        if (this.graphics[key][1] < asof)
            delete this.graphics[key];
    }
}


